/**
 * 获取前端的入参
 * Param: restful API 参数
 * Query: url 参数
 * Body: Post 参数
 */
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Query,
  Body,
  HttpException,
  HttpStatus,
  UseFilters,
} from '@nestjs/common';
import { AppService } from './app.service';
import { HttpExceptionFilter } from './exception/http-exception.filter';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('/data')
  getHello(): string {
    // return this.appService.getHello();
    return 'data';
  }

  @Get('/data/:id/:subId')
  @UseFilters(new HttpExceptionFilter())
  getHelloById(@Param() params): string {
    console.log('params', params);
    // 自定义错误返回
    if (!params.keyId) {
      throw new HttpException('自定义返回的错误', HttpStatus.BAD_REQUEST);
    }
    // return this.appService.getHello();
    return 'data by id' + params.id + ' ' + 'subId:' + params.subId;
  }

  @Post('/data')
  insertedData(@Body() body, @Query() query): string {
    console.log('body', body);
    console.log('query', query);
    // return this.appService.getHello();
    return 'body.a = ' + body.a + ' ' + 'query = ' + query.id;
  }

  @Put('/data')
  putData(@Body() body): string {
    // return this.appService.getHello();
    return 'putData' + JSON.stringify(body);
  }

  @Delete('/data/:id')
  deleteData(@Param() params): string {
    // return this.appService.getHello();
    return 'deleteData: ' + params.id;
  }
}
